package edu.neepu;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import sun.plugin2.os.windows.Windows;

import java.io.*;
import java.net.*;

public class MainClass{
    public static void downImages(String filePath, String imgUrl,String fileName) {
        // 若指定文件夹没有，则先创建
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 写出的路径
        File file = new File(filePath + fileName + ".jpg");

        try {
            // 获取图片URL
            URL url = new URL(imgUrl);
            // 获得连接
            URLConnection connection = url.openConnection();
            // 设置10秒的相应时间
            connection.setConnectTimeout(10 * 1000);
            // 获得输入流
            InputStream in = connection.getInputStream();
            // 获得输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            // 构建缓冲区
            byte[] buf = new byte[1024];
            int size;
            // 写入到文件
            while (-1 != (size = in.read(buf))) {
                out.write(buf, 0, size);
            }
            out.close();
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void test1() {
        for (int i = 1; i < 11; i++) {
            // 利用Jsoup获得连接
            Connection connect = Jsoup.connect("https://bing.ioliu.cn/?p=" + i);
            try {
                // 得到Document对象
                Document document = connect.get();
                // 查找所有img标签
                Elements imgs = document.select(".item");
                for (Element element : imgs) {
                    //获取每个img标签URL "abs:"表示绝对路径
                    Elements download = element.select(".download");
                    String href = download.attr("abs:href");
                    // 打印URL
                    System.out.println("下载地址---"+href);
                    //获取图片名称
                    Elements timeLocation = element.select(".calendar,.location");
                    Elements select = timeLocation.select(".t");
                    String filename = select.text();
                    System.out.println("图片名称---"+filename);

                    //下载图片到本地
                    MainClass.downImages("E:\\imgs\\", href,filename);
                    System.out.println("图片下载完毕");
                    System.out.println("-------------------------------------------------");
                }
                System.out.println("一页下载完成" );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void test() throws IOException {
        Document document = Jsoup.connect("http://www.shicimingju.com/book/").timeout(100000).get();
        Elements elements = document.select(".bookmark-list");
        Elements a = elements.select("a");
        for (Element element : a) {
            File file = new File("E:\\实训\\" + element.text());
            String url1 = element.attr("abs:href");
            if (!file.exists()) {//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            Document document2 = Jsoup.connect(url1).get();
            Elements elements1 = document2.select(".book-mulu");
            Elements a1 = elements1.select("a");
            for (Element element1 : a1) {
                String url2 = element1.attr("abs:href");
                FileWriter fwriter = null;
                String newStrfirst = null;
                String str = null;
                String str2 = null;
                String str3 = null;
                newStrfirst = element1.text().replace("?", " ");
                str = newStrfirst.replace("/", " ");
                str2 = str.replace("\\", " ");
                str3 = str2.replace("\""," ");
                fwriter = new FileWriter("E:\\实训\\" + element.text() + "\\" + str3 + ".txt");
                Document document3 = null;
                try {
                    document3 = Jsoup.connect(url2).get();
                } catch (HttpStatusException e) {
                    e.printStackTrace();
                    continue;
                }
                Elements elements4 = document3.select(".www-main-container");
                for (Element element2 : elements4) {
                    Elements a3 = element2.getElementsByTag("p");
                    try {
                        fwriter.write(a3.text());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } finally {
                        try {
                            fwriter.flush();
                            fwriter.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}